home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 5 / Amiga Tools 5.iso / tools / usefull / lockit / lockit.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-08-13  |  5.3 KB  |  252 lines

  1. #include <clib/alib_protos.h>
  2. #include <clib/dos_protos.h>
  3. #include <clib/exec_protos.h>
  4. #include <clib/console_protos.h>
  5. #include <clib/icon_protos.h>
  6. #include <clib/intuition_protos.h>
  7. #include <clib/macros.h>
  8. #include <devices/input.h>
  9. #include <exec/interrupts.h>
  10. #include <graphics/gfxbase.h>
  11. #include <intuition/intuitionbase.h>
  12. #include <libraries/dosextens.h>
  13. #include <workbench/startup.h>
  14.  
  15. struct NewWindow PassWindow = {
  16.   0,0,0,60,0,1,
  17.   NULL,
  18.   NOCAREREFRESH+SMART_REFRESH+ACTIVATE,
  19.   NULL,NULL,
  20.   NULL,NULL,NULL,
  21.   150,50,640,261,CUSTOMSCREEN };
  22.  
  23. struct Device *ConsoleDevice;
  24. struct IOStdReq *InputReq, *ConsoleReq;
  25. struct MsgPort *LockPort;
  26. struct Interrupt HandlerStuff;
  27.  
  28. struct Task *LockTask;
  29. WORD PassCount, OldPri;
  30. struct Window *Wnd;
  31. BOOL HandlerInstalled, InputOpened, DoGUI;
  32. char *LeadTxt = "Please enter password:", *PassWord = "let me in";
  33. char Entered[55];
  34.  
  35. LONG Result[4];
  36. struct RDArgs *Args;
  37. struct DiskObject *DiskObj;
  38.  
  39. void Center(char *, WORD);
  40. void PrintStar(void);
  41. __geta4 struct InputEvent *HotKeyHandler(__A0 struct InputEvent *, __A1 struct HotStuff *);
  42. void InstallHandler(void);
  43. void RemoveHandler(void);
  44.  
  45. extern struct GfxBase *GfxBase;
  46. extern struct IntuitionBase *IntuitionBase;
  47. extern struct WBStartup *_WBMsg;
  48.  
  49. _main(int argc, char *argv)
  50. {
  51.     if (_WBMsg)
  52.     {
  53.     CurrentDir(_WBMsg->sm_ArgList->wa_Lock);
  54.     if (DiskObj = GetDiskObject(_WBMsg->sm_ArgList->wa_Name))
  55.     {
  56.         char **toolarray;
  57.  
  58.         toolarray = (char **)DiskObj->do_ToolTypes;
  59.  
  60.         Result[0] = FindToolType(toolarray, "GUI");
  61.         Result[1] = FindToolType(toolarray, "LEADTXT");
  62.         Result[2] = FindToolType(toolarray, "PASSWORD");
  63.     }
  64.     } else if ( ! (Args = ReadArgs("GUI/S,LEADTXT,PASSWORD", Result, NULL)) )
  65.     {
  66.     PrintFault(IoErr(), NULL);
  67.     RemoveHandler();
  68.     }
  69.  
  70.     if (Result[0])
  71.     DoGUI = TRUE;
  72.  
  73.     if (Result[1])
  74.     LeadTxt = (char *)Result[1];
  75.  
  76.     if (Result[2])
  77.     PassWord = (char *)Result[2];
  78.  
  79.     LockTask = FindTask(NULL);
  80.     OldPri = SetTaskPri( LockTask, 127);
  81.  
  82.     if (DoGUI)
  83.     {
  84.     PassWindow.Screen = IntuitionBase->ActiveScreen;
  85.     PassWindow.Width = PassWindow.Screen->Width;
  86.     PassWindow.Height = MIN(GfxBase->DefaultFont->tf_YSize * 2 + 30, PassWindow.Screen->Height);
  87.     PassWindow.TopEdge = (PassWindow.Screen->Height - PassWindow.Height) / 2;
  88.     Wnd = OpenWindow(&PassWindow);
  89.     if (!Wnd)
  90.         RemoveHandler();
  91.  
  92.     Center(LeadTxt, GfxBase->DefaultFont->tf_Baseline + 10);
  93.     }
  94.  
  95.     InstallHandler();
  96.     Wait(SIGBREAKF_CTRL_C);
  97.     RemoveHandler();
  98. }
  99.  
  100. void
  101. Center(char *str, WORD y)
  102. {
  103.     WORD textlen = TextLength(Wnd->RPort, str, strlen(str));
  104.  
  105.     SetAPen(Wnd->RPort, 1);
  106.     SetDrMd(Wnd->RPort, JAM2);
  107.     Move(Wnd->RPort, (PassWindow.Width - textlen) / 2, y);
  108.     Text(Wnd->RPort, str, strlen(str));
  109. }
  110.  
  111. void
  112. PrintStar(void)
  113. {
  114.     WORD i = 0;
  115.     char stars[55];
  116.  
  117.     stars[i++] = ' ';
  118.     for (; i<=PassCount; i++)
  119.     stars[i] = '*';
  120.     stars[i++] = ' ';
  121.     stars[i++] = '\0';
  122.  
  123.     Center(stars, GfxBase->DefaultFont->tf_YSize + GfxBase->DefaultFont->tf_Baseline + 20);
  124. }
  125.  
  126. __geta4 struct InputEvent *
  127. HotKeyHandler(__A0 struct InputEvent *HotEvent,__A1 struct HotStuff *HotInfo)
  128. {
  129.     BOOL getout = FALSE;
  130.     struct InputEvent *ev;
  131.  
  132.     Forbid();
  133.     for (ev = HotEvent; ev; ev = ev->ie_NextEvent)
  134.     {
  135.     if (ev->ie_Class == IECLASS_RAWKEY)
  136.         if ( !(ev->ie_Code & IECODE_UP_PREFIX) )
  137.         {
  138.         char t;
  139.  
  140.         if (DoGUI)
  141.         {
  142.             if (ev->ie_Code == 65)
  143.             {
  144.             if (PassCount)
  145.                 PassCount--;
  146.             } else if (RawKeyConvert(ev, &t, 1, NULL) > 0)
  147.             {
  148.             Entered[PassCount] = t;
  149.             if (PassCount < 50)
  150.                 PassCount++;
  151.             }
  152.             PrintStar();
  153.             Entered[PassCount] = 0;
  154.             if (strcmp(PassWord, Entered) == 0)
  155.             getout = TRUE;
  156.         } else
  157.         {
  158.             if (RawKeyConvert(ev, &t, 1, NULL) > 0)
  159.             {
  160.             if (t != PassWord[PassCount])
  161.                 PassCount = (t == PassWord[0]);
  162.             else
  163.                 PassCount++;
  164.  
  165.             if (PassWord[PassCount] == '\0')
  166.                 getout = TRUE;
  167.             }
  168.         }
  169.         }
  170.  
  171.     if ((ev->ie_Class == IECLASS_RAWKEY) || (ev->ie_Class == IECLASS_RAWMOUSE))
  172.         HotEvent = ev->ie_NextEvent;
  173.     }
  174.  
  175.     Permit();
  176.     if (getout)
  177.     Signal(LockTask, SIGBREAKF_CTRL_C);
  178.     return(HotEvent);
  179. }
  180.  
  181. void
  182. InstallHandler(void)
  183. {
  184.     if( !(LockPort = CreatePort(NULL, NULL)))
  185.     RemoveHandler();
  186.     if( !(InputReq = CreateStdIO(LockPort)))
  187.     RemoveHandler();
  188.  
  189.     HandlerStuff.is_Code = (ULONG (*)())HotKeyHandler;
  190.     HandlerStuff.is_Node.ln_Pri = 127;
  191.  
  192.     if(OpenDevice("input.device", NULL, InputReq, NULL) != NULL)
  193.     RemoveHandler();
  194.     InputOpened = TRUE;
  195.  
  196.     InputReq->io_Command = IND_ADDHANDLER;
  197.     InputReq->io_Data = (APTR)&HandlerStuff;
  198.     DoIO(InputReq);
  199.     HandlerInstalled = TRUE;
  200.  
  201.     if( !(ConsoleReq = CreateStdIO(LockPort)))
  202.     RemoveHandler();
  203.     if(OpenDevice("console.device", -1, ConsoleReq, NULL) != NULL)
  204.     RemoveHandler();
  205.     ConsoleDevice = ConsoleReq->io_Device;
  206. }
  207.  
  208. void
  209. RemoveHandler(void)
  210. {
  211.     SetTaskPri( LockTask, OldPri);
  212.  
  213.     if (Args)
  214.     FreeArgs(Args);
  215.  
  216.     if (DiskObj)
  217.     FreeDiskObject(DiskObj);
  218.  
  219.     if (Wnd)
  220.     CloseWindow(Wnd);
  221.  
  222.     if(InputReq)
  223.     {
  224.     if (HandlerInstalled)
  225.     {
  226.         InputReq->io_Command = IND_REMHANDLER;
  227.         InputReq->io_Data = (APTR)&HandlerStuff;
  228.         DoIO(InputReq);
  229.     }
  230.     if (InputOpened)
  231.         CloseDevice(InputReq);
  232.     DeleteStdIO(InputReq);
  233.     }
  234.  
  235.     if (ConsoleReq)
  236.     {
  237.     if (ConsoleDevice)
  238.         CloseDevice(ConsoleReq);
  239.     DeleteStdIO(ConsoleReq);
  240.     }
  241.  
  242.     if (LockPort)
  243.     DeletePort(LockPort);
  244.     _exit(0);
  245. }
  246.  
  247. routine_that_is_never_called()
  248. {
  249.     _waitwbmsg();
  250. }
  251.  
  252.